package com.rbac.util;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 密码测试工具类
 * 用于生成和测试密码加密
 */
public class PasswordTestUtil {
    
    public static void main(String[] args) {
        // 创建BCrypt密码编码器
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        
        // 测试密码
        String rawPassword = "123456";
        
        System.out.println("=== 密码加密测试 ===");
        System.out.println("原始密码: " + rawPassword);
        
        // 生成多个加密密码（每次都会不同）
        for (int i = 1; i <= 5; i++) {
            String encodedPassword = encoder.encode(rawPassword);
            System.out.println("第" + i + "次加密: " + encodedPassword);
            
            // 验证密码
            boolean matches = encoder.matches(rawPassword, encodedPassword);
            System.out.println("验证结果: " + matches);
            System.out.println("---");
        }
        
        // 测试已知的加密密码
        String knownEncodedPassword = "$2a$10$7JB720yubVSOfvVWdBYoOeymRZx1vVHmHvMhA8ThHkc8hnAt.Vj1G";
        System.out.println("测试已知加密密码: " + knownEncodedPassword);
        boolean knownMatches = encoder.matches(rawPassword, knownEncodedPassword);
        System.out.println("已知密码验证结果: " + knownMatches);
        
        if (!knownMatches) {
            System.out.println("已知密码验证失败，生成新的加密密码:");
            String newEncodedPassword = encoder.encode(rawPassword);
            System.out.println("新加密密码: " + newEncodedPassword);
            
            // 验证新密码
            boolean newMatches = encoder.matches(rawPassword, newEncodedPassword);
            System.out.println("新密码验证结果: " + newMatches);
        }
    }
} 